package com.study.samsung.Dao.htDao;

import cn.hutool.core.util.StrUtil;
import com.study.samsung.Entity.User;
import com.study.samsung.Entity.htEntity.Admin;
import com.study.samsung.Utils.Pager;
import com.study.samsung.Utils.RowMapper;
import com.study.samsung.Utils.SQLHelper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
    private SQLHelper helper = new SQLHelper();

    public Admin selectByLogin(String adminaccount){
        String sql = "select * from admin where adminaccount=?";
        return helper.one(sql,new AdminRowMapper(),adminaccount);
    }

    public List<User> selectAllUser() {
        String sql = "select * from user";
        return helper.query(sql, new RowMapper<User>() {
            @Override
            public User map(ResultSet rs) throws SQLException {
                return new User(rs.getInt("uid"),rs.getString("nickname"),
                        rs.getString("uname"),rs.getString("sex"),
                        rs.getString("uphone"),rs.getDate("birth"),
                        rs.getString("upicture"),rs.getString("account"),
                        rs.getString("upwd"),rs.getBoolean("isdel"));
            }
        });
    }

    public Pager rainbow(Integer pageNo, Integer pageSize,String inputvalue) {
        String sql1 = "select count(*) from user where 1=1";
        ArrayList<Object> params = new ArrayList<>();
        StringBuilder stringBuilder = new StringBuilder();
        if (!StrUtil.isEmpty(inputvalue)){
            stringBuilder.append(" and uname like concat('%',?,'%')");
            params.add(inputvalue);
        }
        Integer total = helper.one(sql1 + stringBuilder.toString(), rs -> {
            return rs.getInt(1);
        }, params.toArray());

//        stringBuilder.append(" order by isdel desc");
        stringBuilder.append(" limit ?,?");
        params.add((pageNo-1)*pageSize);
        params.add(pageSize);


        List<User> query = helper.query("select * from user where 1=1" + stringBuilder.toString(), rs -> {
            return new User(rs.getInt("uid"), rs.getString("nickname"),
                    rs.getString("uname"), rs.getString("sex"),
                    rs.getString("uphone"), rs.getDate("birth"),
                    rs.getString("upicture"), rs.getString("account"),
                    rs.getString("upwd"), rs.getBoolean("isdel"));
        }, params.toArray());
        Pager pager = new Pager(pageNo, pageSize);
        pager.setTotal(total);
        pager.setData(query);

        System.out.println(total);
        System.out.println(query);
        return pager;
    }

    public Integer updateStop(Integer uid) {
        String sql = "update user set isdel=0 where uid=?";
        return helper.update(sql, uid);
    }

    public Integer updateStart(Integer uid) {
        String sql = "update user set isdel=1 where uid=?";
        return helper.update(sql, uid);
    }

    public Integer resetImg(Integer uid) {
        String sql = "update user  set upicture='img/personal.png' where uid=?";
        return helper.update(sql,uid);
    }

    class AdminRowMapper implements RowMapper<Admin>{
        @Override
        public Admin map(ResultSet rs) throws SQLException {
            return new Admin(rs.getInt("adminid"),
                             rs.getString("adminaccount"),
                             rs.getString("adminpwd"),
                             rs.getString("adminstate"));
        }
    }
}
